//
//  UPCalenderCustomView.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/26.
//

#import "UPCalenderCustomView.h"

@implementation UPCalenderCustomView

- (instancetype)initWithFrame:(CGRect)frame selectDate: (NSDate *)selectDate
{
    self = [super initWithFrame:frame];
    if (self) {
        self.selectDate = selectDate;
        [self commonInitSubviews];
    }
    return self;
}

- (void)commonInitSubviews {
    // 设置背景图片
    UIImage *bgImage = [UIImage imageNamed:@"calendar_bg"];
    CGFloat bgAspectRatio = bgImage.size.width / bgImage.size.height;
    CGFloat bgWidth = [UIScreen mainScreen].bounds.size.width - 2 * 16;
    CGFloat bgHeight = bgWidth / bgAspectRatio;
    [self.containerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.centerX.equalTo(self);
        make.height.mas_equalTo(bgHeight);
        make.width.mas_equalTo(bgWidth);
        make.bottom.equalTo(self);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.containerImageView).offset(20);
        make.leading.equalTo(self.containerImageView).offset(30);
        make.trailing.equalTo(self.containerImageView).offset(-30);
        make.height.mas_equalTo(25);
    }];
    
    self.calendarPicker = [[UPCalendarPicker alloc] initWithSelectedDate:self.selectDate];
    self.calendarPicker.delegate = self;
    self.calendarPicker.backgroundColor = [UIColor clearColor];
    [self addSubview:self.calendarPicker];
    [self.calendarPicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        make.leading.trailing.bottom.equalTo(self.containerImageView);
    }];
}

- (UIImageView *)containerImageView {
    if (!_containerImageView) {
        _containerImageView = [[UIImageView alloc] init];
        _containerImageView.contentMode = UIViewContentModeScaleAspectFit;
        _containerImageView.image = [UIImage imageNamed:@"calendar_bg"];
        _containerImageView.userInteractionEnabled = YES;
        [self addSubview:_containerImageView];
    }
    return _containerImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = LocalizableForKey(@"Date Selection");
        _titleLabel.font = [UIFont systemFontOfSize:13 weight:400];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (void)calendarPicker:(UPCalendarPicker *)picker didSelectDate:(NSDate *)date {
    if (self.delegate && [self.delegate respondsToSelector:@selector(customCalendarPicker: didSelectDate:)]) {
        [self.delegate customCalendarPicker:picker didSelectDate:date];
    }
}

@end
